package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.util;/**
 * Created by Administrator on 2017/4/5.
 */

import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.DepartmentVo;


import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Ztree;

import java.util.Iterator;
import java.util.List;

/**
 * @Author 卢曼成
 * @Description 部门Util
 * @CreateDateTime 2017-04-05 17:25
 **/
public class DepartmentUtil {


    public static void orgToTree(List<Organization> orgList, List<Organization> depOrgList, List<DepartmentVo> depList, List<DepartmentVo> depItemList, String orgId, List<Ztree> demotree) {
        if (depList != null && depList.size() > 0) {
            for (DepartmentVo dv : depList) {
                if (isDep(dv.getParentOrgId(), orgList, depOrgList)) {
                    continue;
                }
                Ztree demo = new Ztree();
                demo.setId(dv.getDepId());
                demo.setpId(dv.getParentOrgId());
                demo.setName(dv.getDepName());
                demo.setIconSkin(TreeIconSkinEnum.ICON_DEPT.getDesc());
                //判断是否含有下属机构，含有则为true，没有则为false
                demo.setIsParent(false);
                demo.setOpen(false);
                demotree.add(demo);
            }
        }
        if (depOrgList != null && depOrgList.size() > 0) {
            for (Organization org : depOrgList) {
                isContrast(org.getOrgId(), orgList);
                demotree.add(entity(org, orgId));
            }
            if (orgList != null && orgList.size() > 0) {
                for (Organization org : orgList) {
                    demotree.add(entity(org, orgId));
                }
            }
        } else {
            for (Organization org : orgList) {
                Ztree demo = new Ztree();
                demo.setId(org.getOrgId());
                demo.setpId(org.getParentOrgId());
                demo.setName(org.getOrgName());
                demo.setOrgLevel(org.getOrgLevelId());
                int levelId = Integer.parseInt(org.getOrgLevelId());
                demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
                //判断是否含有下属机构，含有则为true，没有则为false
                boolean isParent = isOrg(org.getOrgId(), depItemList.iterator());
                demo.setIsParent(isParent);
                if (org.getOrgId().equals(orgId))
                    demo.setOpen(true);
                else
                    demo.setOpen(false);

                demotree.add(demo);
            }
        }
    }

    /**
     * 组装组织数据
     *
     * @param org
     * @param orgId
     * @return
     */
    private static Ztree entity(Organization org, String orgId) {
        Ztree demo = new Ztree();
        demo.setId(org.getOrgId());
        demo.setpId(org.getParentOrgId());
        demo.setName(org.getOrgName());
        demo.setOrgLevel(org.getOrgLevelId());
        int levelId = Integer.parseInt(org.getOrgLevelId());
        demo.setIconSkin(OrgTreeUtil.iconSkin(levelId));
        //判断是否含有下属机构，含有则为true，没有则为false
        demo.setIsParent(false);
        if (org.getOrgId().equals(orgId))
            demo.setOpen(true);
        else
            demo.setOpen(false);
        return demo;
    }

    /**
     * 验证是否存在的组织部门
     *
     * @param orgId
     * @param orgList
     * @param orgList2
     * @return
     */
    public static boolean isDep(String orgId, List<Organization> orgList, List<Organization> orgList2) {
        if (orgList != null && orgList.size() > 0) {
            for (Organization org : orgList) {
                if (orgId.equals(org.getOrgId())) {
                    return false;
                }
            }
        }
        if (orgList2 != null && orgList2.size() > 0) {
            for (Organization org : orgList2) {
                if (orgId.equals(org.getOrgId())) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 判断当前数据下面是否有子项信息
     *
     * @param orgId
     * @return 有就返回true，没有返回false
     */
    private static boolean isOrg(String orgId, Iterator<DepartmentVo> depIt) {
        if (depIt != null) {
            while (depIt.hasNext()) {
                DepartmentVo param = depIt.next();
                if (param.getParentOrgId().equals(orgId)) {
                    depIt.remove();
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否相同数据
     *
     * @param orgId
     * @param orgList
     * @return
     */
    private static void isContrast(String orgId, List<Organization> orgList) {
        if (orgList != null && orgList.size() > 0) {
            Iterator<Organization> it = orgList.iterator();
            while (it.hasNext()) {
                Organization param = it.next();
                if (param.getOrgId().equals(orgId)) {
                    it.remove();
                }
            }
        }
    }

}
